import { Editor } from 'obsidian';
import { moment } from 'obsidian';
import Toolbox from './main';


export function insertTodaysDate(plugin: Toolbox) {
    plugin.addCommand({
        id: 'insert-todays-date',
        name: 'Insert today\'s date',
        hotkeys: [{ key: "d", modifiers: ["Ctrl", "Alt"] }],
        editorCallback: (editor: Editor) => {
            editor.replaceRange(
                moment().format('YYYY-MM-DD'),
                editor.getCursor()
            );
        },
    });
}

export function insertTodaysTime(plugin: Toolbox) {
    plugin.addCommand({
        id: 'insert-todays-time',
        name: 'Insert today\'s time',
        hotkeys: [{ key: "t", modifiers: ["Ctrl", "Alt"] }],
        editorCallback: (editor: Editor) => {
            editor.replaceRange(
                moment().format('YYYY-MM-DD HH:mm:ss'),
                editor.getCursor()
            );
        },
    });
}

export function convertToUppercase(plugin: Toolbox) {
    plugin.addCommand({
        id: 'convert-to-uppercase',
        name: 'Convert to uppercase',
        hotkeys: [{ key: "u", modifiers: ["Ctrl", "Shift"] }],
        editorCallback: (editor: Editor) => {
            const selection = editor.getSelection();
            editor.replaceSelection(selection.toUpperCase());
        },
    });
}

export function convertToLowercase(plugin: Toolbox) {
    plugin.addCommand({
        id: 'convert-to-lowerrcase',
        name: 'Convert to lowerrcase',
        hotkeys: [{ key: "l", modifiers: ["Ctrl", "Shift"] }],
        editorCallback: (editor: Editor) => {
            const selection = editor.getSelection();
            editor.replaceSelection(selection.toLocaleLowerCase());
        },
    });
}

export function insertFontBlock(plugin: Toolbox) {
    plugin.addCommand({
        id: 'insert-font-block',
        name: 'Insert font block',
        hotkeys: [{ key: "f", modifiers: ["Ctrl", "Alt"] }],
        editorCallback: (editor: Editor) => {
            const left = '<font style="color:red;font-size:1em;font-weight:bold">'
            const right = '</font>'
            const current = editor.getCursor()
            editor.replaceRange(
                `${left}Text${right}`,
                current
            );
            editor.setCursor(current.ch + left.length)
        },
    });
}

export function convertToInlineCode(plugin: Toolbox) {
    plugin.addCommand({
        id: 'convert-to-inline-code',
        name: 'Convert to inline code',
        hotkeys: [{ key: "`", modifiers: ["Ctrl"] }],
        editorCallback: (editor: Editor) => {
            const selection = editor.getSelection();
            editor.replaceSelection("`" + selection + "`");
        },
    });
}